package org.amse.korovkin.ciphering.view;

import java.awt.*;
import java.io.*;
import javax.swing.*;

public class TextJPanel extends JPanel{

    private JTextArea ta = new JTextArea();
    private JScrollPane scroll = new JScrollPane(ta, JScrollPane
            .VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    private JLabel jl;
    private String filePath = null;

    public TextJPanel(String text) {
        ta.setEditable(false);
        //ta.setLineWrap(true);
        ta.setMargin(new Insets(10, 5, 15, 3));
        jl = new JLabel(text);
        jl.setAlignmentX(CENTER_ALIGNMENT);
        scroll.setAlignmentX(CENTER_ALIGNMENT);
        this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        this.add(Box.createVerticalStrut(5));
        this.add(jl);
        this.add(Box.createVerticalStrut(5));
        this.add(scroll);
    }

    public String getFilePath() {
        return filePath;
    }
    public void setFilePath(String string) {
        filePath = string;
    }
    public String getText() {
        return ta.getText();
    }
    public void setText(String text) {
        ta.setText(text);
        ta.setCaretPosition(0);
    }
    public void loadText(String filePath) throws IOException {
        try {
            ta.read(new FileReader(filePath), filePath);
        } catch (IOException ex) {
            throw new IOException("Can not open Input File: " + filePath + " .\n");
        }
    }

}
